package coint.btc.base.controller;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 
 * @ClassName BaseController
 * @author BOOM
 * @date 2018/05/18
 *
 */
@Component
public class BaseController {
	
	private final int port = 80;

    /**
     * SpringMvc下获取response
     *
     * @return
     */
    protected static HttpServletResponse getResponse() {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        return response;

    }

    protected String getServerPath() {
        HttpServletRequest request = getRequest();
        String basePath = request.getScheme() + "://" + request.getServerName() + request.getContextPath();
        if (port != request.getRemotePort()) {
            basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        }
        return basePath;
    }

    protected static HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    public static HttpSession getSession() {
        HttpSession session = getRequest().getSession();
        return session;

    }
}
